/**
 * Created by Administrator on 2016/7/5.
 */

$(function () {
    //详情页面搜索商店信息
    $('.JbusinessInfo').blur(function () {
        var _actShopid, _actShopid, _shophtml = ''
        _actShopid = $(this).val();
        console.log(_actShopid);
        if ($.trim(_actShopid)) {
            $.post(getShopUrl, {_actShopid: _actShopid}, function (data) {
                console.log(data);
                if (data.code == 0) {
                    _shophtml += '<div class="control-group">';
                    $.each(data.data.list, function (key, value) {
                        _shophtml += '<option class="Jshopname" data-shopid="' + value.ShopID + '" data-CityAreaID="' + value.CityAreaID + '" data-city="' + value.City + '">' + value.ShopName + '</option>';
                    })
                    _shophtml += ' </div>';
                    $('.Branchlist').html(_shophtml);
                }
            }, 'json')
        }
    })

    //详情页面点击商店名称
    $('.Branchlist').on("click", '.Jshopname', function () {
        var obj = $(this);
        var parentShopNode = obj.parents(".Branch");
        parentShopNode.find('.shopid').val(obj.text());
        parentShopNode.find('.shopid').attr('shopid', obj.data('shopid'));
        parentShopNode.find('.shopid').attr('cityid', obj.data('city'));
        var shopid = parentShopNode.find('.shopid').attr('shopid');
        var parentNode = parentShopNode.parents('.form-horizontal');
        $.post(getShopService, {shopid: shopid}, function (data) {
            console.log(data);
            console.log(data.Services);
            var str = data.Services;
            if (str.indexOf("1") >= 0 && str.indexOf("6") >= 0) {
                parentNode.find(".radioOne").removeAttr("disabled");
                parentNode.find(".radioSix").removeAttr("disabled");
                parentNode.find(".radioOne").prop('checked', '');
                parentNode.find(".radioSix").prop('checked', '');
                parentNode.find(".shop-follow").addClass('radioOneBlack');
                parentNode.find(".shop-book").addClass('radioSixBlack');
            } else if (str.indexOf("1") >= 0) {
                parentNode.find(".radioOne").removeAttr("disabled");
                parentNode.find(".radioOne").prop('checked', true);
                parentNode.find(".shop-follow").addClass('radioOneBlack');
                parentNode.find(".shop-book").removeClass('radioSixBlack');
                parentNode.find(".radioSix").prop('checked', false);
                parentNode.find(".radioSix").attr("disabled", true);
                parentNode.removeClass('shop-follow');
            } else if (str.indexOf("6") >= 0) {
                parentNode.find(".radioSix").removeAttr("disabled");
                parentNode.find(".radioSix").prop('checked', true);
                parentNode.find(".shop-book").addClass('radioSixBlack');
                parentNode.find(".shop-follow").removeClass('radioOneBlack');
                parentNode.find(".radioOne").prop('checked', false);
                parentNode.find(".radioOne").attr("disabled", true);
                parentNode.removeClass('shop-follow');
            } else {
                parentNode.find(".radioOne").attr("disabled", true);
                parentNode.find(".radioSix").attr("disabled", true);
                parentNode.find(".radioSix").prop('checked', false);
                parentNode.find(".radioOne").prop('checked', false);
                parentNode.find(".shop-follow").removeClass('radioOneBlack');
                parentNode.find(".shop-book").removeClass('radioSixBlack');
            }
        }, 'json')
    })


    //是否显示指定日期
    $("input[type=radio][name=BookDate]").change(function(){
        if($(this).val()==2){
            $('.sureDate').removeAttr('disabled');
        }else{
            $('.sureDate').attr('disabled',true);
            $('.sureDate').val('');
        }
    })

    //新增商店
    $('.JsaveBusiness').click(function () {
        var _businessTitle, _businessDesc, _businessImg, data, _businessName, _shopCityId, _businessid, _shopid;
        var businessTag,shopType,shopSort;
        shopSort = $.trim($('.shop-sort').val());
        if(!shopSort){
            standardTips('请填写排序');
            return;
        }
        if ($(".shopid").attr('shopid') == undefined || !$(".shopid").attr('shopid')) {
            standardTips('请添加店铺');
            return;
        }
        _businessTitle = $.trim($('.shoptitle').val());  //文案标题
        if (!_businessTitle) {
            standardTips('标题不能为空', 2);
            return false
        }
        _businessDesc = $.trim($('.shopdesc').val());  //文案描述
        businessTag = $.trim($('.shopTag').val());
        if(businessTag&&businessTag.length>60){
            standardTips('标签不能大于60个字符');
            return false;
        }
        _businessImg = $('.shoppic').val();  //文案图片
        if(!_businessImg){
            standardTips('请上传图片', 3);
            return false;
        }
        shopType = $("input[type=radio][name=shopfollow]:checked").val();
        if (!shopType) {
            standardTips('请选择业务类型', 2);
            return false;
        }
        _businessName = $(".shopid").val();    //名称
        _shopCityId = $(".shopid").attr('cityid');   //城市
        _businessid = $(".shopid").attr('shopid'); //店铺id
        _shopid = $('.act_all').data('shopid');  //活动id
        data = {
            _businessTitle: _businessTitle,
            _businessDesc: _businessDesc,
            _businessImg: _businessImg,
            _businessName: _businessName,
            _shopCityId: _shopCityId,
            _businessid: _businessid,
            _shopid: _shopid,
            businessTag:businessTag,
            shopType:shopType,
            shopSort:shopSort
        };
        $.post(addBusinessUrl, data, function (data) {
            if (data.errno==0) {
                standardTips('添加成功');
                location.reload();
            }else{
                standardTips(data.errmsg);
            }
        },'json')
    })

    //查出商店信息
    $('.table-list').on('click', '.Jsubedit', function () {
        console.log(now);
        var obj, _id, _html = '';
        obj = $(this);
        _id = obj.data('id');
        $.post(editBusiness, {_id: _id}, function (data) {
            console.log(data);
            if (data) {
                obj.parents('body').find('.Businesstitle').val(data.data.Shop.ShopTitle);
                obj.parents('body').find('.Businesstitle').attr('data-shid', data.data.Shop.ID);
                obj.parents('body').find('.Businessdesc').val(data.data.Shop.ShopDesc);
                _html += '<img title="点击查看大图" style="cursor: pointer;" width="100" hight="100" src="' + IMG_URL+ data.data.Shop.ShopPic + '"/>';
                obj.parents('body').find('.Jmodifypic').html(_html);
                obj.parents('body').find('.businessshoppic').val(data.data.Shop.ShopPic);
                obj.parents('body').find('.getBusinessInfo').text(data.data.Shop.ShopName);
                obj.parents('body').find('.shop-Tag').val(data.data.Shop.ShopTag);
                obj.parents('body').find('.shopSortInfo').val(data.data.Shop.Sort);
                if(data.data.Shop.ShopType==1){
                    obj.parents('body').find('.radioOne').prop('checked',true);
                }else{
                    obj.parents('body').find('.radioSix').prop('checked',true);
                }
            }
        }, 'json')

    })

    //保持商店信息
    $(".JsaveEditBusiness").click(function () {
        var _businessid, _businessTitle, _businessDesc, _businessPic, data, _actId;
        _businessid = $('.Businesstitle').data('shid');
        if (_businessid == '' || !_businessid) {
            standardTips('商家信息不存在', 2);
            return;
        }
        _businessTitle = $.trim($('.Businesstitle').val());
        if (!_businessTitle) {
            standardTips('标题不能为空', 2);
            return false
        }
        _businessDesc = $.trim($('.Businessdesc').val());
        _businessPic = $('.businessshoppic').val();
        if (!_businessPic) {
            standardTips('图片不能为空', 2);
            return false
        }
        _actId = $('.act_all').data('shopid');
        var sort = $(".shopSortInfo").val();
        if (!sort) {
            standardTips('排序不能为空', 2);
            return false
        }
        var shopTag = $(".shop-Tag").val();
        if(shopTag&&shopTag.length>60){
            standardTips('标签不能大于60个字符');
            return false;
        }

        data = {
            _businessid: _businessid,
            _businessTitle: _businessTitle,
            _businessDesc: _businessDesc,
            _businessPic: _businessPic,
            _actId: _actId,
            sort:sort,
            shopTag:shopTag
        };
        $.post(saveEditBusiness, data, function (data) {
            if (data.errno==0) {
                standardTips('修改成功', 2);
                location.reload();
            }else{
                standardTips(data.errmsg, 2);
                return false;
            }
        }, 'json')
    })

    //新增详情页
    var f = 1,b=0;
    $('.JaddSpecialPushList').click(function(){
        var len = $('.Jshoplist').find('.act-border').length;
        b = f+len;
        console.log(b);
        if(b>6){
            standardTips('最多添加6个');
            return false;
        }
        var html='';
        html += '<div class="control-group  act-border">' +
            '<div class="controls text-center  ">' +
            '<label class="Jshopdelete">' +
            '<span class="del-list" style="cursor: pointer">&nbsp;删除</span></label>' +
            '</div> ' +
            '<div class="control-group">' +
            '<label class="control-label">上传大图片</label>' +
            '<div class="controls">' +
            '<div class="coverBigImgInfo">' +
            '<form class="setBigLinkForm" action="' + coverBigImg + '" method="post" enctype="multipart/form-data">' +
            '<input type="file" id="coverImg" name="coverBigImg" accept="image/*" onChange="coverImgChangeBigLink(this,1);">' +
            '<span class="coverbiglinkFormImg"></span></form>' +
            '<span class="form-required" >*</span> ' +
            '</div>' +
            '</div>' +
            '</div>' +
            '<div class="control-group">' +
            '<label class="control-label">上传小图片</label>' +
            '<div class="controls">' +
            '<div class="coverSmallImgInfo">' +
            '<form class="setSmallLinkForm" action="' + coverSmallImg + '" method="post"enctype="multipart/form-data">' +
            '<input type="file" id="coverImg" name="coverSmallImg" accept="image/*" onChange="coverImgChangeSmallLink(this,1);">' +
            '<span class="coverSmalllinkFormImg"></span></form>' +
            '<span class="form-required" >*</span> ' +
            '</div>' +
            '</div>' +
            '</div>' +
            '<div class="control-group">' +
            '<label class="control-label">专题活动链接：</label>' +
            '<div class="controls">' +
            '<input type="text" name="linkUrl" class="linkUrl">' +
            '</div></div>';
        $(".Jshoplist").append(html);
        b++;
    })


    //详情页删除
    $(".Jshoplist").on('click', '.act-border .del-list', function () {
        var _obj = $(this);
        standardConfirm('确定删除', function () {
            _obj.parents('.act-border').remove();
            b--;
        })
    })

    //编辑推荐专题
    $('.JsaveBusinessLink').click(function(){
        var linkUrl = $('.Jshoplist').find('.act-border').length;
            var _actId = $('.act_all').data('shopid');
            var picFlag = true;
            var link = [];
            $('.Jshoplist .act-border').each(function (i, target) {
                $(target).each(function (i, n) {
                    var stark = {};
                    if (!$(target).find('.bigPicInfo:eq(' + i + ')').val()) {
                        standardTips('请添加大图片');
                        picFlag = false;
                        return false
                    }
                    if (!$(target).find('.smallPicInfo:eq(' + i + ')').val()) {
                        standardTips('请添加小图片');
                        picFlag = false;
                        return false
                    }
                    if (!$(target).find('.linkUrl:eq(' + i + ')').val()) {
                        standardTips('请添加链接');
                        picFlag = false;
                        return false
                    }
                    var rg = /(https?|http):\/\/([^\.\/]+)([\.]?)([^\.\/]+)([\.]?)([^\.\/]+)(\/[\w\-\.\/\?\%\&\=]*)?/i;
                    if ($(target).find('.linkUrl:eq(' + i + ')').val() && !rg.test($(target).find('.linkUrl:eq(' + i + ')').val())) {
                        standardTips('url格式错误');
                        picFlag = false;
                        return false;
                    }
                    stark.bigpic = $(target).find('.bigPicInfo:eq(' + i + ')').val();
                    stark.smallpic = $(target).find('.smallPicInfo:eq(' + i + ')').val();
                    stark.linkurl = $(target).find('.linkUrl:eq(' + i + ')').val();
                    stark.linkid = $(n).attr('linkid');
                    console.log(stark);
                    link.push(stark);
                })
            })
            if (!picFlag) {
                return false
            }
            var data={link:link,Actid:_actId};
            $.post(specialLink,data,function(data){
                console.log(data);
                if(data.errno==0){
                    standardTips('修改成功');
                    location.reload();
                }else{
                    standardTips(data.errmsg);
                }
            },'json')
    })

    $('.JcopyUrl').click(function () {
        var _url = $('.JcopyUrl').data('url');
        standardAllow(_url, function () {

        })
    })

    $('.Jviewacturl').click(function(){
        var _url=[],_obj=$(this),_city=[],_data='';
        _obj.parents('.wrap').find('.shop-url').each(function(i,n){
            _url.push($(this).val());
            _city.push($(this).data('city'));
        })

        for(i=0;i<_url.length;i++){
            _data+=_city[i]+':'+ _url[i]+'<br/>';
        }

        standardAllow(_data, function () {

        });
    })
})


//详情页添加图片
function coverBusinessImgChange(obj) {
    var file = $(obj).val();
    if(!file){
        return false;
    }
    $("#imgBussinessForm").ajaxSubmit({
        dataType: 'json',
        beforeSubmit: function () {
            $("#coverShopImg").html('上传中……');
        },
        success: function (data) {
            if (data.res == 0) {
                $("#coverShopImg").html('<a target="_blank" href=' + IMG_URL+data.info + '>预览</a>');
                $(obj).parents("#coverBusinessImgInfo").find(".shoppic").val(data.info);
                standardTips('上传成功！');
            } else {
                alert(data.error);
            }
        }
    });
}

/**
 * 编辑页面coverBunsiessImg
 */
function coverEditBusinessImgChange(obj) {
    var file = $(obj).val();
    if(!file){
        return false;
    }
    $("#imgBussiness").ajaxSubmit({
        dataType: 'json',
        beforeSubmit: function () {
            $(obj).parents("#coverBusinessInfo").find("#coverBunsiessImg").html('上传中……');
        },
        success: function (data) {
            if (data.res == 0) {
                $(obj).parents("#coverBusinessInfo").find("#coverBunsiessImg").html('<a target="_blank" href=' + IMG_URL+data.info + '>预览</a>');
                $(obj).parents("#coverBusinessInfo").find(".businessshoppic").val(data.info);
                $(obj).parents(".form-horizontal").find(".Jmodifypic img").attr('src',IMG_URL+data.info);
                standardTips('上传成功！');
            } else {
                alert(data.error);
            }
        }
    });
}
